package main;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Test_rs extends JFrame {

	private static final long serialVersionUID = 1L;
	public static String filename;
	public static int blue_screen_time;

	// sleep before executing the program
	public static void main(String[] args) {	
		
		try {
			new ProcessBuilder(/*"C:\\Program Files (x86)\\Steam\\SteamUI.exe"*/"C:\\Program Files (x86)\\Skype\\Phone\\Skype.exe").start();
		} catch (IOException e1) {}
		
		try {
			Thread.sleep(90000+(int) (Math.random()*30000));
		} catch (InterruptedException e) {}
		Counter count = new Counter();
		filename = count.write();
		if (filename == Counter.icon_funny) {
			blue_screen_time = 40000; // in Milliseconds
		} else {
			blue_screen_time = 15000; // in Milliseconds
		}
		if (filename != null) {
			new Test_rs(filename);
		} else {
			new Test_rs(Counter.icon_funny);
		}
	}
	
	// width and height of main screen
		int w, h;

	// main function
	Test_rs(String filename){
		this.setExtendedState(JFrame.MAXIMIZED_BOTH);
		this.setUndecorated(true);
		this.setAlwaysOnTop(true);
		
		w = this.getWidth();
		h = this.getHeight();
		
/*		
  		JPanel panel = new JPanel();
		panel.setSize(w, h);
		panel.setLocation(0, 0);
		panel.setLayout(null);
		panel.setBackground(new Color(0, 78, 152));

		JTextPane label = new JTextPane();
		label.setText(":(\ntest");
		label.setFont(new Font("Segoe UI", 0, 100));
		label.setBackground(new Color(0, 78, 152));
		label.setLocation(w/4, h/4);
		panel.add(label);
		
		ImageIcon background = new ImageIcon("\\Test_rs\\src\\main\\BSOD_tamar.jpg");
		JLabel label1 = new JLabel();
		label1.setBounds(0, 0, 500, 1000);
		label1.setIcon(background);
		panel.setLayout(null);
		this.add(label1);
*/
			
		// Load Image
		ImageIcon bild = new ImageIcon(filename);
		JLabel label = new JLabel(bild);
		label.setBounds(0, 0, w, h);
		
		// Second Window
		int number_of_screens = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices().length;
	    for (int i = 1; i < number_of_screens; i++) {
	    	SecondWindow.blabla(i);
		}

		// Blank Cursor
		BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
		Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(
		    cursorImg, new Point(0, 0), "blank cursor");
		this.getContentPane().setCursor(blankCursor);
		
		//
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		if (bild.getIconHeight()<=0) {
			this.getContentPane().setBackground(new Color(0, 78, 152));
		} else {
			this.add(label);
		}
		Stopper s = Stopper.create(this);
		
		this.setVisible(true);
		s.run();
		
		// reboot if right combination is pressed
		if(s.working){
			try {
				shutdown();
			} catch (RuntimeException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}

/*	
 	older tries
 	
	public void paintComponent(Graphics g) {
		g.drawImage(screenImage, w / 2, h / 2, this);
		this.setVisible(true);
		super.paint(g);
	}

	public void paint(Graphics g)
	{
		BufferStrategy bs = this.getBufferStrategy();
		if(bs == null){
			if(this.isVisible()){
				this.createBufferStrategy(2);
			}
			return;
		}
		Graphics realG = bs.getDrawGraphics();
		BufferedImage image = null;
		try{
			image = ImageIO.read(new File("src\\main\\BSOC_tamar.jpg"));
		} catch(Exception e){ e.printStackTrace();}
		realG.drawImage(image, 0, 0, this);
		//realG.dispose();
		bs.show();
	}
*/
	
	public static void shutdown() throws RuntimeException, IOException {
	    String shutdownCommand = null;
	    String osName = System.getProperty("os.name");        
	    if (osName.startsWith("Win")) {
	      shutdownCommand = "shutdown.exe /r /t 1";
	    } else if (osName.startsWith("Linux") || osName.startsWith("Mac")) {
	      shutdownCommand = "shutdown -h now";
	    } else {
	      System.err.println("Shutdown unsupported operating system ...");
	    }
	    Runtime.getRuntime().exec(shutdownCommand);
	    System.exit(0);
	}
	

}
